define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'code/index' + location.search,
                    add_url: 'code/add',
                    edit_url: 'code/edit',
                    del_url: 'code/del',
                    multi_url: 'code/multi',
                    table: 'code',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1')}, formatter: Table.api.formatter.status},
                        {field: 'code', title: __('Code')},
                        // {field: 'user_id', title: __('User_id')},

                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

                    //绑定TAB事件
            $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                // var options = table.bootstrapTable(tableOptions);
                var typeStr = $(this).attr("href").replace('#', '');
                var options = table.bootstrapTable('getOptions');
                options.pageNumber = 1;
                options.queryParams = function (params) {
                    // params.filter = JSON.stringify({type: typeStr});
                    params.type = typeStr;

                    return params;
                };
                table.bootstrapTable('refresh', {});
                return false;

            });
        },

        add: function () {
            $('#auto_code').on('click', function (e) {
               $.ajax({
                   url: "code/auto_code",
                   dataType: "json",
                   async: true,
                   type: "POST",
               }).done(function (data) {
                    var code = data.msg;
                    document.getElementById("c-code").value =  code ;
               })
            });
            Controller.api.bindevent();
            setTimeout(function () {
                $("#c-type").trigger("change");
            }, 100);
        },
        edit: function () {
            $('#auto_code').on('click', function (e) {
               $.ajax({
                   url: "code/auto_code",
                   dataType: "json",
                   async: true,
                   type: "POST",
               }).done(function (data) {
                    var code = data.msg;
                    document.getElementById("c-code").value =  code ;
               })
            });
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                $(document).on("change", "#c-type", function () {
                    $("#c-category_id option[data-type='all']").prop("selected", true);
                    $("#c-category_id option").removeClass("hide");
                    $("#c-category_id option[data-type!='" + $(this).val() + "'][data-type!='all']").addClass("hide");
                    $("#c-category_id").data("selectpicker") && $("#c-category_id").selectpicker("refresh");
                });
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});